package daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import dao.BackUserDao;
import dao.UserDao;

import pojo.BackUser;
import utils.DateUtils;
import utils.DruidUtils;


public class BackUserDaoImpl implements BackUserDao{
	
	/*
	 ************************************注册部分***********************************
	 */
	
	/**
	 * 根据号码获取user表中对应的user实体，
	 * 然后UserService层根据返回的实体是否为空来
	 * 判断要注册的号码已经存在与数据库中
	 */
	@Override
	public BackUser getUserByUserPhoneNum(String userPhoneNumber) {
		Connection conn = DruidUtils.getConnection();
		String sql = "select * from userInfo where userPhoneNumber = ?";
		BackUser u =null;
		try {
			PreparedStatement pre = conn.prepareStatement(sql);
			pre.setString(1, userPhoneNumber);
			ResultSet res = pre.executeQuery();
			while(res.next()) {
				int userId = res.getInt("userId");
				String userName = res.getString("userName");
				String password = res.getString("password");
				int manage = res.getInt("manage");
				String phone = res.getString("userPhoneNumber");
				Date createTime = res.getDate("createTime");
				u = new BackUser(userId,userName,password,manage,phone,createTime);
			}
			res.close();
			pre.close();
			conn.close();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return u;
	}

	@Override
	public int savaUser(BackUser user) {
		Connection conn = DruidUtils.getConnection();
		String sql = "insert into userInfo values(0,?,?,?,?,?)";
		PreparedStatement pre = null;
		try {
			pre = conn.prepareStatement(sql);
			pre.setString(1, user.getUserName());
			pre.setString(2,user.getPassword());
			pre.setInt(3, user.getManage());
			pre.setString(4, user.getPhoneNumber());
			pre.setString(5, DateUtils.formDate(user.getCreateTime(), "yyyy-MM-dd hh:mm:ss"));
			return pre.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				pre.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	@Override
	public BackUser getUserByUserName(String username) {
		Connection conn = DruidUtils.getConnection();
		String sql = "select * from userInfo where userName = ? ";
		BackUser u = null;
		try {
			PreparedStatement pre = conn.prepareStatement(sql);
			pre.setString(1, username);
			ResultSet res = pre.executeQuery();
			while(res.next()) {
				int userId = res.getInt("userId");
				String userName = res.getString("userName");
				String password = res.getString("password");
				int manage = res.getInt("manage");
				String phone = res.getString("userPhoneNumber");
				Date createTime = res.getDate("createTime");
				u = new BackUser(userId,userName,password,manage,phone,createTime);
			}
			res.close();
			pre.close();
			conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return u;
	}
	
	/*
	 ************************************登入部分***********************************
	 */
	
	@Override
	public BackUser getUserByNamePsw(String userName, String password) {
		Connection conn = DruidUtils.getConnection();
		String sql = "select * from userInfo where userName = ? and password = ? ";
		BackUser u = null;
		try {
			PreparedStatement pre = conn.prepareStatement(sql);
			pre.setString(1, userName);
			pre.setString(2, password);
			ResultSet res = pre.executeQuery();
			while(res.next()) {
				int userId = res.getInt("userId");
				String un = res.getString("userName");
				String psw = res.getString("password");
				int manage = res.getInt("manage");
				String phone = res.getString("userPhoneNumber");
				Date createTime = res.getDate("createTime");
				u = new BackUser(userId,un,psw,manage,phone,createTime);
			}
			res.close();
			pre.close();
			conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return u;
	}
	
}
